package cn.com.darly.videoandroidpm.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.darly.common.ToastApp;
import com.darly.dview.common.SCfg;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.header.TitleView;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.PopInfo;
import cn.com.darly.videoandroidpm.common.ClassCache;
import cn.com.darly.videoandroidpm.common.SystemLoginInfo;
import cn.com.darly.videoandroidpm.ui.adapter.PMPopAdapter;
import cn.com.darly.videoandroidpm.ui.fragment.CenterFragment;
import cn.com.darly.videoandroidpm.ui.fragment.HomeFragment;
import cn.com.darly.videoandroidpm.ui.fragment.OrnamentalFlowerFragment;
import cn.com.darly.videoandroidpm.ui.fragment.SucculentFragment;
import cn.com.darly.videoandroidpm.ui.fragment.VenusaurFragment;
import cn.com.darly.videoandroidpm.widget.theme.ButtonRadio;

/**
 *  项目启动页面，或者会是一个加载过度页面
 * Copyright (c) 2017 Organization D.L. zhangyuhui All rights reserved.
 * @author  Darly/张宇辉/2018/4/19 10:49
 * @version  1.0/cn.com.darly.videoandroidpm
 */
@ContentBinder(R.layout.activity_pm)
public class PMActivity extends BaseActivity implements View.OnClickListener {
    @ViewsBinder(R.id.id_base_title)
    public TitleView id_base_title;
    @ViewsBinder(R.id.id_main_footer_main)
    private ButtonRadio id_main_footer_main;
    @ViewsBinder(R.id.id_main_footer_list)
    private ButtonRadio id_main_footer_list;
    @ViewsBinder(R.id.id_main_footer_conversion)
    private ButtonRadio id_main_footer_conversion;
    @ViewsBinder(R.id.id_main_footer_act)
    private ButtonRadio id_main_footer_act;
    @ViewsBinder(R.id.id_main_footer_center)
    private ButtonRadio id_main_footer_center;
    /**
     * 下午2:19:21 TODO 首页
     */
    // private FragmentMain main;

    private HomeFragment main;
    /**
     * 下午2:19:29 TODO 列表页
     */
    private OrnamentalFlowerFragment list;

    /**
     * 上午10:19:18 TODO 消息列表
     */
    private SucculentFragment conver;

    /**
     * 下午2:19:42 TODO 活动页
     */
    private VenusaurFragment act;

    /**
     * 下午2:19:51 TODO 设置页
     */
    private CenterFragment center;


    //PopupWindow
    private PopupWindow pop;
    private ListView listView;
    private PMPopAdapter adapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        id_base_title.setTitle(R.string.app_name);
        id_base_title.removeBackground(R.drawable.ic_title_background);

        id_main_footer_main.setInfo(R.string.footer_main, R.drawable.ic_main_index);
        id_main_footer_act.setInfo(R.string.footer_act, R.drawable.ic_main_video);
        id_main_footer_conversion.setInfo(R.string.footer_msg, R.drawable.ic_main_i);
        id_main_footer_list.setInfo(R.string.footer_list, R.drawable.ic_main_friend);
        id_main_footer_center.setInfo(R.string.footer_center, R.drawable.ic_main_set);
        //開啟權限控制
        if (SystemLoginInfo.getboolean(SystemLoginInfo.ROLE)){
            id_main_footer_conversion.setVisibility(View.VISIBLE);
        }else {
            id_main_footer_conversion.setVisibility(View.GONE);
        }
    }


    @Override
    protected void loadData() {
        adapter = new PMPopAdapter(ClassCache.getPopInfo(),R.layout.item_pop_pm,this);
    }

    @Override
    protected void initListener() {

        id_base_title.setRightViewRightTextOneListener("十",this);
        id_main_footer_main.setOnClickListener(this);
        id_main_footer_act.setOnClickListener(this);
        id_main_footer_conversion.setOnClickListener(this);
        id_main_footer_list.setOnClickListener(this);
        id_main_footer_center.setOnClickListener(this);
        swichTo(R.id.id_main_footer_main);
    }


    /**
     * 下午2:22:23
     *
     * @author zhangyh2 TODO
     */
    private void toFirstPage() {
        // TODO Auto-generated method stub
        id_base_title.setTitle(R.string.footer_main);
        id_main_footer_main.isChecked(true);
        id_main_footer_list.isChecked(false);
        id_main_footer_conversion.isChecked(false);
        id_main_footer_act.isChecked(false);
        id_main_footer_center.isChecked(false);
        fragmentManager = getSupportFragmentManager();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        hideFragments(ft);
        TAGSS = HomeFragment.class.getSimpleName();

        if (main != null) {
            if (main.isVisible())
                return;
            ft.show(main);
        } else {
            main = new HomeFragment();
            ft.add(R.id.id_main_frame, main, TAGSS);
        }
        ft.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_view_ok:
                if(pop == null){
                    listView = new ListView(this);
                    listView.setCacheColorHint(0x00000000);
                    listView.setDividerHeight(1);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            //在這裡設置跳轉
                            pop.dismiss();
                            try {
                                PopInfo info = (PopInfo) parent.getItemAtPosition(position);
                                if (info.isLogin()){
                                    if (SystemLoginInfo.getboolean(SystemLoginInfo.LOGIN)){
                                        //已经登录，直接跳转
                                        Intent intent = new Intent();
                                        intent.setClass(PMActivity.this,Class.forName(info.getClazz()));
                                        startActivity(intent);
                                    }else {
                                        startActivity(new Intent(PMActivity.this, LoginActivity.class));
                                    }
                                }else {
                                    Intent intent = new Intent();
                                    intent.setClass(PMActivity.this,Class.forName(info.getClazz()));
                                    startActivity(intent);
                                }
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    pop = new PopupWindow(listView, SCfg.getWidth()/2, AbsListView.LayoutParams.WRAP_CONTENT, true);
                    pop.setFocusable(true);
                    pop.showAsDropDown(v,0, 0);
                }else{
                    if(!pop.isShowing()){
                        pop.showAsDropDown(v, 0, 0);
                    }else{
                        pop.dismiss();
                    }
                }
                break;
            default:
                swichTo(v.getId());
                break;
        }

    }

    /**
     * 下午3:41:54
     *
     * @author zhangyh2 TODO
     */
    public void swichTo(int resid) {
        id_main_footer_main.isChecked(false);
        id_main_footer_list.isChecked(false);
        id_main_footer_conversion.isChecked(false);
        id_main_footer_act.isChecked(false);
        id_main_footer_center.isChecked(false);
        fragmentManager = getSupportFragmentManager();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (resid) {
            case R.id.id_main_footer_main:
                hideFragments(ft);
                TAGSS = HomeFragment.class.getSimpleName();
                id_main_footer_main.isChecked(true);
                id_base_title.setTitle(R.string.footer_main);
                if (main != null) {
                    if (main.isVisible())
                        return;
                    ft.show(main);
                } else {
                    main = new HomeFragment();
                    ft.add(R.id.id_main_frame, main, TAGSS);
                }
                break;
            case R.id.id_main_footer_act:
                // 观花类
                hideFragments(ft);
                TAGSS = VenusaurFragment.class.getSimpleName();
                id_main_footer_act.isChecked(true);
                id_base_title.setTitle(R.string.footer_act);
                if (act != null) {
                    if (act.isVisible())
                        return;
                    ft.show(act);
                } else {
                    act = new VenusaurFragment();
                    ft.add(R.id.id_main_frame, act, TAGSS);
                }

                break;
            case R.id.id_main_footer_list:
                // 芳香类
                if (!SystemLoginInfo.getboolean(SystemLoginInfo.LOGIN)) {
                    startActivity(new Intent(this, LoginActivity.class));
                    // id_main_footer_main.setChecked(true);
                    toFirstPage();
                    return;
                } else {
                    hideFragments(ft);
                    TAGSS = OrnamentalFlowerFragment.class.getSimpleName();
                    id_main_footer_list.isChecked(true);
                    id_base_title.setTitle(R.string.footer_list);
                    if (list != null) {
                        if (list.isVisible())
                            return;
                        ft.show(list);
                    } else {
                        list = new OrnamentalFlowerFragment();
                        ft.add(R.id.id_main_frame, list, TAGSS);
                    }
                }
                break;
            case R.id.id_main_footer_conversion:
                // 观果类
                hideFragments(ft);
                TAGSS = SucculentFragment.class.getSimpleName();
                id_main_footer_conversion.isChecked(true);
                id_base_title.setTitle(R.string.footer_msg);
                if (conver != null) {
                    if (conver.isVisible())
                        return;
                    ft.show(conver);
                } else {
                    conver = new SucculentFragment();
                    ft.add(R.id.id_main_frame, conver, TAGSS);
                }
                break;
            case R.id.id_main_footer_center:
                if (!SystemLoginInfo.getboolean(SystemLoginInfo.LOGIN)) {
                    startActivity(new Intent(this, LoginActivity.class));
                    // id_main_footer_main.setChecked(true);
                    toFirstPage();
                    return;
                } else {
                    hideFragments(ft);
                    TAGSS = HomeFragment.class.getSimpleName();
                    id_main_footer_center.isChecked(true);
                    id_base_title.setTitle(R.string.footer_center);
                    if (center != null) {
                        if (center.isVisible())
                            return;
                        ft.show(center);
                    } else {
                        center = new CenterFragment();
                        ft.add(R.id.id_main_frame, center, TAGSS);
                    }
                }
                break;
            default:
                break;
        }
        ft.commit();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction
     *            用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (main != null) {
            transaction.hide(main);
        }
        if (list != null) {
            transaction.hide(list);
        }
        if (conver != null) {
            transaction.hide(conver);
        }
        if (act != null) {
            transaction.hide(act);
        }
        if (center != null) {
            transaction.hide(center);
        }
    }

    private long time = 0;

    /*
     * (non-Javadoc)
     *
     * @see android.support.v4.app.FragmentActivity#onKeyDown(int,
     * android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            long dely = System.currentTimeMillis();
            if (dely - time < 2000) {
                System.exit(0);
            } else {
                ToastApp.showToast("再次点击退出程序");
                time = dely;
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
